<template>
  <module
    ref="module"
    class="equipment-information"
    :borderConfig="borderConfig"
    :moduleConfig="moduleConfig"
    :octagon="octagon"
    :barLineConfigTop="barLineConfigTop"
    :pyramidTop="pyramid"
    :gaugeBottom="equipmentGauge"
    :gaugeBottom2="inspectionGauge"
    :gaugeBottom3="maintainGauge">
  </module>
</template>

<script>
import Module from './../components/Module.vue'
export default {
  name: 'equipmentEnergy',
  components: {
    Module
  },
  data () {
    return {
      borderConfig: {
        moduleType: '1x1',
        icon: '',
        titleText: '设备能耗',
        goPageUrl: '/home/equipment'
      },
      moduleConfig: {
        isOctagon: true, // 是否需要头部的八边形
        topType: '13', // 第一层显示内容
        bottomType: '222' // 第二层显示内容
      },
      barLineConfigTop: {
        title: {
          text: '近一年水电趋势'
        },
        axis: {
          yAxisConversion: 'w', // Y轴坐标%
          xAxisDataType: 1, // X轴坐标从当前月份开始
          xAxisLabelInterval (index, name) {
            if (index % 2 === 1) {
              return name
            }
          }
        },
        yAxis: {
        },
        tooltipsConfig: {
          tipsColor: ['#2e92cb', '#b60d81'],
          isNearlyYear: true // 月份是否是当前月份开始
        },
        seriesData: []
      },
      octagon: {
        iconClass: ['icon-shebei', 'icon-shebeiyichang'],
        octagonName: ['正常设备', '异常设备'],
        octagonNumber: [0, 0],
        octagonUnit: ['个', '个']
      },
      equipmentGauge: {
        title: {
          text: '本月设备完好率',
          bottom: 0
        },
        angle: '',
        radius: '90%'
      }, // 本月设备完好率
      inspectionGauge: {
        title: {
          text: '本月巡检完成率',
          bottom: 0
        },
        angle: '',
        radius: '90%'
      }, // 本月巡检完成率
      maintainGauge: {
        title: {
          text: '本月保养完成率',
          bottom: 0
        },
        angle: '',
        radius: '90%'
      }, // 本月保养完成率
      pyramid: {
        title: '本月水电情况',
        imgIndex: [3],
        tipsMessage: [{
          beforeMessage: '本月耗水量',
          message: '',
          unit: ''
        }, {
          beforeMessage: '本月耗电量',
          message: '',
          unit: ''
        }]
      },
      energyInformation: {}, // 设备耗水数据
      equipmentInformation: {}, // 设备信息
      refreshInterval: 0
    }
  },
  computed: {
    organId () {
      return this.$store.state.organId
    },
    isIndex () {
      return this.$store.state.isIndex
    }
  },
  methods: {
    showTips  (data) {
      this.$parent.showTips(data)
    },
    getData (type, refreshInterval, isRefresh) {
      if (type === 'init') {
        this.equipmentApi.getHydropowerEnergyInfo({ organId: this.organId }).then(res => {
          if (res.data.code === '0') {
            let data = res.data.data
            this.energyInformation = data
            this.pyramid.tipsMessage[0].message = data.waterCount
            this.pyramid.tipsMessage[1].message = data.electCount
          } else {
            this.energyInformation = {}
            this.pyramid.tipsMessage[0].message = 0
            this.pyramid.tipsMessage[1].message = 0
            this.$parent.showTips('请求失败，点击确认继续刷新')
          }
          if (!isRefresh) {
            this.handleData()
          }
        }, res => {
          this.energyInformation = {}
          this.pyramid.tipsMessage[0].message = 0
          this.pyramid.tipsMessage[1].message = 0
          this.$parent.showTips('请求失败，点击确认继续刷新')
          if (!isRefresh) {
            this.handleData()
          }
        })
        this.equipmentApi.getEquipInspectionMaintainInfo({ organId: this.organId }).then(res => {
          if (res.data.code === '0') {
            let data = res.data.data
            this.octagon.octagonNumber = [data.normalCount, data.excepCount]
            this.equipmentGauge.angle = data.equipWholeRate.indexOf('%') > -1 ? data.equipWholeRate.replace('%', '') : ''
            this.inspectionGauge.angle = data.inspCompRate.indexOf('%') > -1 ? data.inspCompRate.replace('%', '') : ''
            this.maintainGauge.angle = data.mainCompRate.indexOf('%') > -1 ? data.mainCompRate.replace('%', '') : ''
          } else {
            this.octagon.octagonNumber = [0, 0]
            this.equipmentGauge.angle = 0
            this.inspectionGauge.angle = 0
            this.maintainGauge.angle = 0
            this.$parent.showTips('请求失败，点击确认继续刷新')
          }
        }, res => {
          this.octagon.octagonNumber = [0, 0]
          this.equipmentGauge.angle = 0
          this.inspectionGauge.angle = 0
          this.maintainGauge.angle = 0
          this.$parent.showTips('请求失败，点击确认继续刷新')
        })
        if (refreshInterval) {
          this.refreshInterval = refreshInterval
          this.automaticRedrawing()
        }
      } else {
        this.handleData()
      }
    },
    handleData () {
      let data = this.energyInformation
      let waterArr = []
      let electricityArr = []
      if (JSON.stringify(data) !== '{}') {
        data.hisMths.forEach(item => {
          waterArr.push(item.hisWaterCount)
          electricityArr.push(item.hisElectCount)
        })
      }
      this.barLineConfigTop.seriesData = [
        {
          data: waterArr,
          name: '耗水量',
          colorIndex: 0
        },
        {
          data: electricityArr,
          name: '耗电量',
          colorIndex: 1
        }
      ]
    },
    resizeChart () {
      this.$refs.module.resizeChart()
    },
    // 根据接口返回的时间间隔请求接口
    automaticRedrawing () {
      clearInterval(window.equipmentEnergyTimer)
      if (this.refreshInterval > 0 && this.isIndex) {
        window.equipmentEnergyTimer = setInterval(() => {
          this.getData('init', '', true)
        }, this.refreshInterval * 1000)
      }
    }
  },
  mounted () {
  },
  destroyed () {
    clearInterval(window.equipmentEnergyTimer)
  }
}
</script>
<style lang="scss" scoped>
.equipment-information {
  .module-content {
    width: 100%;
    height: 100%;
  }
  .module-left {
    width: 70%;
    height: 100%;
    float: left;
  }
  .module-right {
    width: 30%;
    height: 65%;
    transform: translate(0, 46%);
    float: left;
    position: relative;
    & /deep/ .singleRing-title {
      position: absolute;
      left: 0;
      bottom: 4%;
      color: #fff;
    }
  }
}
</style>
